'use server'
import { NextRequest, NextResponse } from "next/server";
import pool from "@/lib/db";

export async function GET(request: NextRequest) {
  // 根据colum 列中的数据'谭客户' 查询数据
  const result = await pool.query('SELECT * FROM xiaolu WHERE colum = $1', ['谭客户']);
  return NextResponse.json(result.rows);
}

export async function POST(request: NextRequest) {
  try {
    const { name, colum, bz } = await request.json();

    // 插入数据到 xiaolu 表中
    const result = await pool.query(
      'INSERT INTO xiaolu (name, colum, bz) VALUES ($1, $2, $3) RETURNING *',
      [name, colum, bz]
    );

    // 检查 result 是否存在且 rowCount 是否大于 0
    if (result && result.rowCount !== undefined && result.rowCount > 0) {
      return NextResponse.json({ message: '插入成功' }, { status: 201 });
    } else {
      return NextResponse.json({ message: '插入失败' }, { status: 500 });
    }
  } catch (error) {
    console.error('插入数据时出错:', error);
    return NextResponse.json({ message: '插入失败' }, { status: 500 });
  }
}